//
//  MineViewController.m
//  TeacherNet
//
//  Created by slz on 2017/12/19.
//  Copyright © 2017年 bnsr. All rights reserved.
//

#import "MineViewController.h"


@interface MineTableViewCell : UITableViewCell
@property (nonatomic,strong) UILabel *aaaLabel;
@property (nonatomic,strong) UIImageView *iconImageView;
@end

@implementation MineTableViewCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self){
        _iconImageView = [UIImageView new];
        [self.contentView addSubview:_iconImageView];
        [_iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.contentView.mas_left).offset(24);
            make.centerY.mas_equalTo(self.contentView.mas_centerY);
            make.size.mas_equalTo(CGSizeMake(23, 23));
        }];
        
        _aaaLabel = [UILabel new];
        _aaaLabel.textColor = kDark_TextColor_Default;
        _aaaLabel.fontSize = kFont_Size_Small_Title;
        [self.contentView addSubview:_aaaLabel];
        [_aaaLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(_iconImageView.mas_right).offset(12);
            make.centerY.mas_equalTo(_iconImageView.mas_centerY);
        }];
        
        UIImageView * arrow = [[UIImageView alloc]init];
        arrow.image = [UIImage imageNamed:@"mine_arrow"];
        [self.contentView addSubview:arrow];
        [arrow mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(self.contentView.mas_right).offset(-24);
            make.centerY.mas_equalTo(self.contentView.mas_centerY);
            make.size.mas_equalTo(CGSizeMake(16, 16));
        }];
        
        UIView * lineView = [UIView quickLineColorView];
        [self.contentView addSubview:lineView];
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.and.bottom.mas_equalTo(self.contentView);
            make.height.mas_equalTo(.5);
        }];
        
    }
    return self;
}
@end

@interface MineViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) YWTableView *tableView;
@property (strong, nonatomic)  UIImageView *userImageVIew;
@property (strong, nonatomic)  UILabel *userNameLabel;
@property (strong, nonatomic)  UILabel *fanceLabel;
@property (strong, nonatomic)  UILabel *concernLabel;
@property (nonatomic,strong) NSArray *titles;
@property (nonatomic,strong) NSArray *images;
@property (nonatomic,strong) NSArray *controllers;
@property (strong, nonatomic)  UIView *tableHeaderView;

@end

@implementation MineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.showNavigationBar = YES;
    [self setControllerTitle:@"个人中心"];
    _titles = @[@"我的消息",@"我的收藏",@"个人信息",@"系统设置"];
    _images = @[@"mine_tableview_icon1",@"mine_tableview_icon2",@"mine_tableview_icon3",@"mine_tableview_icon4"];
    _controllers = @[@"UserMessageViewController",@"UserCollectViewController",@"UserInfoViewController",@"MoreSettingViewController"];
    [self setUpViews];

    // Do any additional setup after loading the view from its nib.
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self requestWithUserInfo];
}

- (void)setUpViews
{
    self.tableView = [[YWTableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.view);
    }];
    
    _tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 130)];
    _tableHeaderView.backgroundColor = [UIColor blackColor];
    
    self.userImageVIew = [[UIImageView alloc]init];
    [self.tableHeaderView addSubview:_userImageVIew];
    [_userImageVIew mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.tableHeaderView.mas_left).offset(24);
        make.centerY.mas_equalTo(self.tableHeaderView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(70, 70));
    }];
    
    _userNameLabel = [UILabel new];
    _userNameLabel.fontSize = kFont_Size_Small_Title;
    _userNameLabel.textColor = kLoginGreenColor;
    [_tableHeaderView addSubview:_userNameLabel];
    [_userNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_userImageVIew.mas_right).offset(12);
        make.top.mas_equalTo(_userImageVIew.mas_top).offset(3);
    }];

    _fanceLabel = [UILabel new];
    _fanceLabel.textColor = [UIColor whiteColor];
    _fanceLabel.text = @"粉丝：0";
    _fanceLabel.fontSize = kFont_Size_Cotent_Nomal;
    [_tableHeaderView addSubview:_fanceLabel];
    [_fanceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(_userImageVIew.mas_bottom).offset(-3);
        make.left.mas_equalTo(_userNameLabel.mas_left);
    }];

    _concernLabel = [UILabel new];
    _concernLabel.textColor = [UIColor whiteColor];
    _concernLabel.fontSize = kFont_Size_Cotent_Nomal;
    _concernLabel.text = @"关注：0";
    [_tableHeaderView addSubview:_concernLabel];
    [_concernLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(_userImageVIew.mas_bottom).offset(-3);
        make.left.mas_equalTo(_fanceLabel.mas_right).offset(10);
    }];
    
    [self updateUserInfo];
}

- (void)updateUserInfo
{
    MFWUserInfo * uInfo = [MFWSdkEnvironment sharedInstance].userInfo;
    [self.userImageVIew sd_setImageWithURL:[NSURL URLWithString:kAPI_IMAGE_HOST(uInfo.logoUrl)] randomPlaceHolder:YES];
    self.userImageVIew.layer.cornerRadius = 35;
    self.userImageVIew.layer.masksToBounds = YES;
    self.userImageVIew.layer.borderWidth = 2;
    self.userImageVIew.layer.borderColor = [UIColor whiteColor].CGColor;
    self.userNameLabel.text = uInfo.nickName;
    
    self.tableView.tableHeaderView = _tableHeaderView;
}

- (void)requestWithUserInfo
{
    [BaseHttpTool get:kAPI_USER_INFO params:@{@"user_id":[MFWSdkEnvironment sharedInstance].userInfo.uid} success:^(id responseObj) {
        MFWUserInfo *userinfo=[MFWSdkEnvironment sharedInstance].userInfo;
        
        if ([userinfo updateInfoFromUserJsonData:[MFWJson jsonWithObject:responseObj]]){
            [self updateUserInfo];
        }
    } failure:^(NSError *error) {
        
    }];
}



#pragma mark - UITableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _titles.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * reuserIdentifier = @"MineTableViewCell";
    MineTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuserIdentifier];
    if (!cell){
        cell = [[MineTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuserIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    cell.aaaLabel.text = _titles[indexPath.row];
    cell.iconImageView.image = [UIImage imageNamed:_images[indexPath.row]];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    Class class = NSClassFromString(_controllers[indexPath.row]);
    UIViewController * controller = [[class alloc]init];
    if ([controller isKindOfClass:[TTBaseViewController class]]){
        [self.navigationController pushViewController:controller animated:YES];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
